// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CowRun from "./CowRun";
import StarCollector from "./StarCollector";

const {ccclass, property} = cc._decorator;

@ccclass
export default class GameManager extends cc.Component {

    canvasWidth: number;
    canvasHeight: number;

    @property(StarCollector)
    starCollector: StarCollector;

    @property([cc.SpriteFrame])
    ropeImgs: cc.SpriteFrame[] = [];

    // 分数
    score: number = 0;

    @property(cc.Node)
    cowRoot: cc.Node;

    @property(cc.Node)
    gameOver: cc.Node;

    @property(cc.Prefab)
    cowPreb: cc.Prefab;

    @property(cc.Node)
    cowRope: cc.Node;

    @property(cc.Label)
    countDownLB: cc.Label;

    @property
    countDuration: number = 0;  // 倒计时总时长

    @property
    interval: number = 0;   // 单位间隔，单位s

    // 是否在抛绳子
    isThrowing: boolean = false;

    onLoad () {
        this.enabled = true;
        let canvas = cc.find('Canvas');
        this.canvasWidth = canvas.width;
        this.canvasHeight = canvas.height;
        this.gameOver.active = false;
    }

    start () {
        this.beginSchedule();
        this.cowRope.getComponent(cc.Sprite).spriteFrame = this.ropeImgs[0];

        this.createCowSchedule();
    }

    update (dt) {

    }

    // 检测是否套住了牛
    checkTakeCow() {
        for (let index = 0; index < this.cowRoot.childrenCount; index++) {
            let cow = this.cowRoot.children[index];
            if (cow.active == true && (cow.x >= 50 && cow.x <= 160)) {
                return cow;
            }
        }

        return null;
    }

    getRopeType(cowType) {
        if (cowType == 0) {
            return 3;
        }

        if (cowType == 1) {
            return 1;
        }

        if (cowType == 2) {
            return 2;
        }

        return 0;
    }

    // 套牛的动作
    throwCow() {
        if (this.isThrowing) {
            return;
        }

        this.cowRope.getComponent(cc.Sprite).spriteFrame = this.ropeImgs[0];
        this.isThrowing = true;

        // 默认在屏幕外
        this.cowRope.y = - this.canvasHeight; 

        // 移出去
        let mTop = cc.moveTo(0.5, cc.v2(0, 68 - this.cowRope.height/2));

        // 套住牛
        let mFun = cc.callFunc(function() {
            // 判断是否套住了牛
            let cow = this.checkTakeCow();
            if (cow) {
                let cowType = cow.getComponent('CowRun').getCowType();

                // 由于动作
                let ropeType = this.getRopeType(cowType);
                console.log("cowType:" + cowType + ",ropeType:" + ropeType);
                // 更换绳子样式
                this.cowRope.getComponent(cc.Sprite).spriteFrame = this.ropeImgs[ropeType];

                cow.active = false;
                cow.removeFromParent();

                // 更新分数
                this.score++;
                
                // 创建牛
                this.createCow();
            }
        }.bind(this));

        // 拖回
        let md = cc.moveTo(0.5, cc.v2(0, -this.canvasHeight));

        // 动作结束
        let finish = cc.callFunc(function() {
            this.isThrowing = false;
            this.starCollector.updateScore(this.score);
        }.bind(this));
        
        let seq = cc.sequence([mTop, mFun, md, finish]);
        this.cowRope.runAction(seq);
    }
    
    onDestroy() {
        this.stopSchedule();
    }

    // 牛跑动
    createCowSchedule() {
        this.createCow();

        // 随机3~5秒;
        var time = 5 + Math.random() * 2;
        this.scheduleOnce(this.createCowSchedule.bind(this), time);
    }

    createCow() {
        let cow = cc.instantiate(this.cowPreb);
        this.cowRoot.addChild(cow);
        cow.setPosition(cc.v2(this.canvasWidth, -60));
    }

    
    // 倒计时
    beginSchedule() {
        this.countDuration = 100;
        this.schedule(this.onCountDown, this.interval);
    }

    onCountDown() {
        this.countDuration--;
        if (this.countDuration < 0) {
            // 游戏结束，弹出游戏结束提示
            this.showGameOver();

            this.stopSchedule();

            this.countDuration = 0;
        }
        
        this.countDownLB.string = this.countDuration + "s";
    }

    stopSchedule() {
        this.unschedule(this.onCountDown);
    }

    // 游戏结束
    showGameOver() {
        this.enabled = false;
        this.gameOver.active = true;
    }
}
